package com.uzhie.web.service;

import com.uzhie.web.bean.PageBean;
import com.uzhie.web.dao.CatalogDAO;
import com.uzhie.web.dao.EquipmentDAO;
import com.uzhie.web.dao.VehicleDAO;
import com.uzhie.exception.EquipmentException;
import com.uzhie.exception.UserException;
import com.uzhie.web.module.Catalog;
import com.uzhie.web.module.Equipment;
import com.uzhie.web.module.Vehicle;
import com.uzhie.util.ConstantKit;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 用户service,封装与用户相关的服务操作
 * <p>
 * Created by lilongfei on 2018/7/12.
 */
@Service
public class EquipmentService {

    @Autowired
    private EquipmentDAO equipmentDAO;
    @Autowired
    private VehicleDAO vehicleDAO;
    @Autowired
    private CatalogDAO catalogDAO;

    /**
     * 根据条件查询设备信息
     *
     * @param deviceNumber  设备序列号
     * @param simNumber     sim卡号
     * @param mfrsName      生产厂商
     * @param deviceType    设备类型
     * @param minCreateTime 开始时间
     * @param maxCreateTime 结束时间
     * @return
     */
    public List<Equipment> selectAllByCondition(@NotEmpty(message = UserException.Status.ID_ISNULL) String userId, String deviceNumber, String simNumber, String mfrsName, String deviceType, String minCreateTime, String maxCreateTime, String departName, PageBean pageBean) {
        List<Catalog> departs = catalogDAO.selectCatalogByUserId(userId);
        List<String> departIds = new ArrayList<>();
        for (Catalog depart : departs) {
            departIds.add(depart.getId());
        }
        List<Equipment> list = equipmentDAO.selectAllByCondition(departIds, deviceNumber, simNumber, mfrsName, deviceType, minCreateTime, maxCreateTime, departName, pageBean.getRowBounds());
        return list;
    }

    /**
     * 添加设备信息
     *
     * @param equipment 设备信息
     */
    public void addEquipment(Equipment equipment) throws EquipmentException {
        try {
            equipment.setCreateTime(new Date());
            equipment.setStatus(ConstantKit.STATUS.IS_AVAILABLE);
            if (equipment.getSimNumber()!=null) {
                Boolean b = equipmentDAO.simExist(equipment.getSimNumber());
                if (b)  throw new EquipmentException(EquipmentException.Status.EQUPMENT_EXIST);
            }
            int t = equipmentDAO.insertSelective(equipment);
            if (t != 1) throw new EquipmentException(EquipmentException.Status.INSERT_ERROR);
        } catch (Exception e) {
            Map<String, Object> idStatus = equipmentDAO.selectIDStatus(equipment.getSimNumber());
            if (idStatus != null && (new Integer(ConstantKit.STATUS.DELETED)).equals(idStatus.get("status"))) {
                equipment.setId((String) idStatus.get("id"));
                equipmentDAO.updateByPrimaryKeySelective(equipment);
            } else {
                throw new EquipmentException(EquipmentException.Status.INSERT_ERROR);
            }
        }
    }

    /**
     * 根据id,查询设备信息详情
     *
     * @param equipmentId
     * @return
     */
    public Equipment selectEquipmentById(String equipmentId) {
        return equipmentDAO.selectEquipmentById(equipmentId);
    }

    /**
     * 更新设备信息
     *
     * @param equipment
     * @throws EquipmentException
     */
    public void updateEquipment(@NotNull(message = EquipmentException.Status.EQUIPMENT_ISNULL) @Valid Equipment equipment) throws EquipmentException {
        //更新服务器时间
        equipment.setUpdateTime(new Date());
        if (equipment.getStatus() == 0 ) equipmentDAO.deleteByPrimaryKey(equipment.getId());
        else {
            int t = equipmentDAO.updateByPrimaryKeySelective(equipment);
            vehicleDAO.updateDepart(equipment.getVehicleNumber(), equipment.getDepartId(), equipment.getDepartName());
            if (t != 1) throw new EquipmentException(EquipmentException.Status.UPDATA_ERROR);

        }
    }

    /**
     * 根据设备id,更新设备状态信息
     *
     * @param equipmentIds
     * @param status
     * @return
     */
    public void updateEquipmentStatusById(List<Equipment> equipmentIds, Integer status) throws EquipmentException {
        if (equipmentIds.size() == 0) throw new EquipmentException(EquipmentException.Status.ID_ISNULL);
        for (Equipment equipment : equipmentIds) {
            if (status == 0) equipmentDAO.deleteByPrimaryKey(equipment.getId());
            else {
                int t = equipmentDAO.updateEquipmentStatusById(equipment.getId(), status);
                if (t == 0) throw new EquipmentException(EquipmentException.Status.UPDATA_STATUS_ERROR);
            }
        }
    }

    /**
     * 设备绑定车辆信息
     *
     * @param equipmentId
     * @param vehicleId
     */
    public void bindVehicles(String equipmentId, String vehicleId) throws EquipmentException {

        //查询车辆信息
        Vehicle vehicle = vehicleDAO.selectVehicleById(vehicleId);
        //更新设备信息
        Equipment equipment = new Equipment();
        equipment.setId(equipmentId);
        equipment.setVehicleId(vehicleId);
        equipment.setVehicleNumber(vehicle.getNumber());
        equipment.setVehicleType(vehicle.getType());
        //更新车辆信息

        int t = equipmentDAO.updateByPrimaryKeySelective(equipment);
        if (t == 0) throw new EquipmentException(EquipmentException.Status.UPDATA_BIND_VEHICLE);
    }

    /**
     * 取消设备绑定车辆
     *
     * @param equipmentId
     * @throws EquipmentException
     */
    public void unbindVehicles(String equipmentId) throws EquipmentException {

        //更新设备信息
        Equipment equipment = new Equipment();
        equipment.setId(equipmentId);
        equipment.setVehicleId("");
        equipment.setVehicleNumber("");
        equipment.setVehicleType("");
        int t = equipmentDAO.updateByPrimaryKeySelective(equipment);
        if (t == 0) throw new EquipmentException(EquipmentException.Status.UPDATA_BIND_VEHICLE);
    }

    /**
     * @param sim sim
     * @return true 在 false 不在
     * @author 唐飞
     * <p>
     * 检测SIM卡是否在数据库中
     */
    public boolean simExist(String sim) {
        return equipmentDAO.simExist(sim);
    }
}


